package com.info.zhiduoduo.core.formbean.mqproducer;

import com.alibaba.fastjson.JSON;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

@Service("producer")
public class Producer {

    @Autowired // 也可以注入JmsTemplate，JmsMessagingTemplate对JmsTemplate进行了封装
    private JmsMessagingTemplate jmsTemplate;

    // 发送消息，destination是发送到的队列，message是待发送的消息
    public void sendMessage(Destination destination, final String message){
        jmsTemplate.convertAndSend(destination, message);
    }

    /**
     * 发送短信
     *
     * @param phoneMessage
     */
    public void sendSms(PhoneMessage phoneMessage){

        /*EmailMessage emailMessage = new EmailMessage();
        emailMessage.setToAddress("93148348@qq.com");
        emailMessage.setContent("验证码：53325");
        emailMessage.setTitle("邮件验证码");*/

        Destination destination =  new ActiveMQQueue("Zdd_SmsQueue");

        String message = JSON.toJSONString(phoneMessage);

        jmsTemplate.convertAndSend(destination, message);

    }

    /**
     * 发送到mq
     * @param mqName
     * @param jsonStr
     */
    public void sendMq(String mqName,String jsonStr){
        Destination destination =  new ActiveMQQueue(mqName);
        jmsTemplate.convertAndSend(destination, jsonStr);
    }

    /**
     * 发送 推送
     *
     * @param pushMessage
     */
    public void sendPush(PushMessage pushMessage){

        Destination destination =  new ActiveMQQueue("Zdd_PushQueue");

        String message = JSON.toJSONString(pushMessage);

        jmsTemplate.convertAndSend(destination, message);

    }
}
